798. Smallest Rotation with Highest Score
798. Smallest Rotation with Highest Score
Description
Solution
Let’s first check how many times we need take to move ith element
to nums[i]
position where is the first index that gets point.
Take nums = [2,3,1,4, 0] as example:
1 | A[0] = 2 move to 2's index, k = 3 = (0 - A[0] + 5) % 5 |
For one element, after move k
times, it have reached to the point that one more step move will make it lose point. So we define a array to store the index that all element firstly lose point.
And we could find that when K = k
, A[i] is happened to lost point, then k = k+1, A[i] still can’t get point, but the first element on the head of the array rotate back to the tail will get new point.
So we have the recurrence relation:
1 | dp[k+1] += dp[k] - 1 (Note we are counting the numer of losing point's element) |
Code
1 | class Solution { |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.